"""
查找替换嵌套的 replace 函数
"""

import logging
import re


def change_date_sub_unix_date(m):
    new_date_format = "floor(extract(epoch from TO_CHAR(CURRENT_DATE - INTERVAL '{} DAY', 'yyyyMMdd')::timestamp))*1000".format(
        str(m.group(6)).strip())
    return new_date_format


def change_date_sub_unix_date2(m):
    new_date_format = "floor(extract(epoch from CURRENT_DATE - INTERVAL '{} DAY'))*1000".format(str(m.group(6)).strip())
    return new_date_format


def change_date_sub_month_unix_date(m):
    # # unix_timestamp(date_sub(now(),INTERVAL 3 MONTH))*1000
    new_date_format = "floor(extract(epoch from TO_CHAR(CURRENT_DATE - INTERVAL '{} MONTH', 'yyyyMMdd')::timestamp))*1000".format(str(m.group(5)).strip())
    return new_date_format


def change_date_add_day_unix_date(m):
    new_date_format = "floor(extract(epoch from TO_CHAR(CURRENT_DATE - INTERVAL '{} DAY', 'yyyyMMdd')::timestamp))*1000".format(str(m.group(5)).strip())
    return new_date_format


def change_date_add_unix_date(m):
    new_date_format = "floor(extract(epoch from TO_CHAR(CURRENT_DATE + INTERVAL '{} DAY', 'yyyyMMdd')::timestamp))*1000".format(str(m.group(6)).strip())
    return new_date_format


def change_date_add_unix_date2(m):
    logging.info("change_date_add_unix_date2")
    logging.debug(m)
    new_date_format = "floor(extract(epoch from TO_CHAR(CURRENT_DATE + INTERVAL '{} DAY', 'yyyyMMdd')::timestamp))*1000".format(
        str(m.group(6)).strip())
    return new_date_format


def rep_complex_fn_unix_timestamp(document_content):
    logging.info("rep_complex_fn_unix_timestamp")
    unix_timestamp_patten = re.compile(
        r'(unix_timestamp|UNIX_TIMESTAMP)\((date_sub|DATE_SUB)\((date_format|DATE_FORMAT)\((NOW|now)\(\)\,\'\%Y\%m\%d\'\)\,(interval|INTERVAL) (.*) (day|DAY)\)\)\*1000')
    result = unix_timestamp_patten.sub(change_date_sub_unix_date, document_content)
    document_content = result

    # unix_timestamp(date_sub(date_format(now(), '%Y-%m-%d'), interval 100 day)) * 1000
    unix_timestamp_patten = re.compile(
        r"(unix_timestamp|UNIX_TIMESTAMP)\((date_add|DATE_ADD)\((date_format|DATE_FORMAT)\((NOW|now)\(\)\,\'\%Y\-\%m\-\%d\'\)\,(interval|INTERVAL) (.*) (day|DAY)\)\)\*1000")
    result = unix_timestamp_patten.sub(change_date_add_unix_date2, document_content)
    document_content = result

    unix_timestamp_patten = re.compile(
        r"(unix_timestamp|UNIX_TIMESTAMP)\((date_sub|DATE_SUB)\((date_format|DATE_FORMAT)\((NOW|now)\(\)\,\'\%Y\-\%m\-\%d\'\)\,(interval|INTERVAL) (.*) (day|DAY)\)\)\*1000")
    result = unix_timestamp_patten.sub(change_date_sub_unix_date2, document_content)
    document_content = result

    # unix_timestamp(date_sub(now(),INTERVAL 3 MONTH))*1000
    unix_timestamp_patten = re.compile(
        r'(unix_timestamp|UNIX_TIMESTAMP)\((date_sub|DATE_SUB)\((NOW|now)\(\)\,(interval|INTERVAL) (.*) (MONTH|month)\)\)\*1000')
    result = unix_timestamp_patten.sub(change_date_sub_month_unix_date, document_content)
    document_content = result

    # unix_timestamp(date_add(now(), interval 1 day)) * 1000
    unix_timestamp_patten = re.compile(
        r'(unix_timestamp|UNIX_TIMESTAMP)\((date_add|DATE_ADD)\((now|NOW)\(\)\,(interval|INTERVAL) (.*) (day|day)\)\)\*1000')
    result = unix_timestamp_patten.sub(change_date_add_day_unix_date, document_content)
    document_content = result

    unix_timestamp_patten = re.compile(
        r'(unix_timestamp|UNIX_TIMESTAMP)\((date_add|DATE_ADD)\((date_format|DATE_FORMAT)\((NOW|now)\(\)\,\'\%Y\%m\%d\'\)\,(interval|INTERVAL) (.*) (day|DAY)\)\)\*1000')
    result = unix_timestamp_patten.sub(change_date_add_unix_date, document_content)
    document_content = result

    return document_content
